/**
 * Created by leiqr on 2015/9/22.
 */
define(function (require, exports, module) {
    require("angular-ui");
    require("angular-route");
    require("permission");
    var angular = require("angular");
    require("angular-interceptor");
    require("pagination");


    var urlPrefix = window['G_CONFIG']['api_prefix'] + 'qmyx/broker/';

    angular.module('app', ['ngRoute', 'ngInterceptor', 'ngUI'])
        .config(['$routeProvider', function ($routeProvider) {
            $routeProvider
                .when('/channel', {templateUrl: ''})
                .when('/mobile', {templateUrl: ''})
                .otherwise({redirectTo: '/channel'});
        }])
        .factory('httpPost', ['$http', function ($http) {
            return function (url, data) {
                if (!data) {
                    data = {};
                }
                data = $.extend(data, {"USE_FORM_DATA": false});
                return $http.post(url, data);
            }
        }])
        .service('cstServiceList', ['$q', 'httpPost', function ($q, httpPost) {
            var service = {
                getProjectList: function (queryData) {
                    var getParams = {
                        pageIndex: queryData.pageIndex,
                        pageSize: queryData.pageSize
                    };
                    var deferred = $q.defer();
                    httpPost(urlPrefix + '&' + $.param(getParams), queryData)
                        .success(function (res) {
                            if (res.retCode == 0) {
                                deferred.resolve(res.data);
                            }
                        });
                    return deferred.promise;
                },
                getCstList: function (queryData) {
                    var getParams = {
                        pageIndex: queryData.pageIndex,
                        pageSize: queryData.pageSize
                    };
                    var deferred = $q.defer();
                    httpPost(urlPrefix + '&' + $.param(getParams), queryData)
                        .success(function (res) {
                            if (res.retCode == 0) {
                                deferred.resolve(res.data);
                            }
                        });
                    return deferred.promise;
                }
            };
            return service;
        }])
        .controller('MainController', ['$rootScope', '$scope', '$location', '$route', '$routeParams', 'cstServiceList',
            function ($rootScope, $scope, $location, $route, $routeParams, cstServiceList) {

                //cstServiceList.getProjectList().then(function(data){
                //    $scope.projectList = data;
                //});

                $scope.tabs = [
                    {value: '/channel', name: '渠道洗客'},
                    {value: '/mobile', name: '移动日常跟客'}
                ];
                $scope.current = $location.path();

                $rootScope.$on('$routeChangeSuccess', function (evt, cur, pre) {
                    $scope.current = $location.path();
                });

                $scope.projectList = [{
                    "p_projectId": "39d29507-be9a-9476-1f79-fc746fa855ad",
                    "ProjName": "AAA"
                }, {
                    "p_projectId": "39d29505-be9a-9476-1f79-fc746fa855ad",
                    "ProjName": "bbb"
                }];

                //$scope.curProjectId

                var json = {
                    data: [{
                        mobile: '111',
                        cst_name: 'name'
                    }, {
                        mobile: '21122',
                        cst_name: 'name'
                    }, {
                        mobile: '3122',
                        cst_name: 'name'
                    }, {
                        mobile: '4122',
                        cst_name: 'name'
                    }, {
                        mobile: '125',
                        cst_name: 'name'
                    }], total: 5
                };


                $scope.filter = {
                    startDate: null,
                    endDate: null,
                    pageSize: 2
                };


                $scope.pageIndex = 1;
                $scope.tableList = json.data;
                $scope.total = $scope.tableList.length;


                $scope.getCstList = function () {

                    cstServiceList.getCstList({
                        pageIndex: $scope.pageIndex,
                        pageSize: $scope.filter.pageSize
                    }).then(function (data) {
                        $scope.tableList = data;
                    }).finally(function () {

                    });

                };


                $scope.getFilterData = function (pageIndex) {
                    $scope.pageIndex = 2;
                };

                $scope.getDataPage = function () {


                };

                function update(curProjectId) {
                    $scope.getCstList();
                }

                $scope.$watch('curProjectId', function (newValue, oldValue) {
                    if (newValue !== oldValue) {
                        update(newValue);
                    }
                })

            }
        ])
        .controller('CustomerSourceCtrl', ['$rootScope', '$scope', '$location', '$route', '$routeParams',
            function ($rootScope, $scope, $location, $route, $routeParams) {

                $scope.sourceArr = [
                    {value: '1', name: '外购客户'},
                    {value: '2', name: '小蜜蜂客户'},
                    {value: '3', name: '预约客户'}
                ];

                $scope.currentType = $scope.sourceArr[0].value;

                $scope.changeCustomerType = function (source) {
                    $scope.currentType = source.value;
                }


            }]);

    angular.bootstrap(document, ['app']);
});
